//
//  FBMedical_HUD.m
//  FBMedical
//
//  Created by 马乾征 on 2017/10/10.
//  Copyright © 2017年 YJ XML. All rights reserved.
//

#import "FBHUD.h"
#import "FBIndicatorHUD.h"
#import "MBProgressHUD.h"
@implementation FBHUD
+ (void)loading {
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [self loadingInView:keyWindow];
}

+ (void)loadingInView:(UIView *)view {
    [self loadingInView:view message:nil];
}

+ (void)loadingInView:(UIView *)view message:(NSString *)message {
    for (UIView *hud in view.subviews) {
        if ([hud isKindOfClass:[MBProgressHUD class]]) {
            return;
        }
    }
    FBIndicatorConfig *hud = [FBIndicatorHUD activityHUD];
    hud.config.Message(message);
    view.userInteractionEnabled = NO;
    [hud showInView:view];
}

+ (void)end {
    [FBIndicatorHUD hidden];
}

+ (void)endInView:(UIView *)view {
    view.userInteractionEnabled = YES;
    [FBIndicatorHUD hiddenForView:view];
}
@end

// 小菊花
@interface BTNetDisk_ActivityIndicatorView : UIActivityIndicatorView
@end
@implementation BTNetDisk_ActivityIndicatorView
@end

// 小菊花
@interface FBActivity ()

@end

@implementation FBActivity
+ (BTNetDisk_ActivityIndicatorView *)activityIndicatorView {
    return [[BTNetDisk_ActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
}

+ (void)loading {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow] ? : [[UIApplication sharedApplication].delegate window];
    [self loadingInView:keyWindow];
}

+ (void)end {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow] ? : [[UIApplication sharedApplication].delegate window];
    [self endInView:keyWindow];
}

+ (void)loadingInView:(UIView *)view {
    if (!view) {
        return;
    }
    for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[BTNetDisk_ActivityIndicatorView class]]) {
            return;
        }
    }
    UIActivityIndicatorView *activityView = [self activityIndicatorView];
    view.userInteractionEnabled = NO;
    [view addSubview:activityView];
    activityView.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *activityViewCenterXConstraint = [NSLayoutConstraint constraintWithItem:activityView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    NSLayoutConstraint *activityViewCenterYConstraint = [NSLayoutConstraint constraintWithItem:activityView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    [view addConstraints:@[activityViewCenterXConstraint, activityViewCenterYConstraint]];
    [activityView startAnimating];
}

+ (void)endInView:(UIView *)view {
    if (!view) {
        return;
    }
    BTNetDisk_ActivityIndicatorView *activityView = nil;
    for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[BTNetDisk_ActivityIndicatorView class]]) {
            activityView = (BTNetDisk_ActivityIndicatorView *)subview;
        }
    }
    if (activityView) {
        view.userInteractionEnabled = YES;
        [activityView stopAnimating];
        [activityView removeFromSuperview];
    }
}

@end
